package com.example.tch.case05_16110100521;

import android.support.constraint.Group;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ImageButton ib1,ib2,ib3;
    private TextView tv1,tv2,tv3;
    private Group gp1,gp2,gp3;
    private ViewPager vp1;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib1=(ImageButton)findViewById(R.id.ib1);
        ib2=(ImageButton)findViewById(R.id.ib2);
        ib3=(ImageButton)findViewById(R.id.ib3);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
/*        gp1 = findViewById(R.id.group1);
        gp2 = findViewById(R.id.group2);
        gp3 = findViewById(R.id.group3);*/

        vp1=(ViewPager)findViewById(R.id.viewpager1);
        initEvents();
        initFragment();

    }
    private void resetImage()
    {
        ib1.setImageResource(android.R.drawable.btn_star_big_off);
        ib2.setImageResource(android.R.drawable.btn_star_big_off);
        ib3.setImageResource(android.R.drawable.btn_star_big_off);

    }
    private void selectTab(int i)
    { switch (i)
    { case 0:ib1.setImageResource(android.R.drawable.btn_star_big_on);
        break;
        case 1:ib2.setImageResource(android.R.drawable.btn_star_big_on);
            break;
        case 2:ib3.setImageResource(android.R.drawable.btn_star_big_on);
            break;
    }
        vp1.setCurrentItem(i);
    }

    View.OnClickListener onClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
//先将四个按钮都设成未选图片
            resetImage();
            switch (view.getId())
            {
                case R.id.ib1:
                case R.id.tv1:
                    selectTab(0); break;
                case R.id.ib2:
                case R.id.tv2:
                    selectTab(1); break;

                case R.id.tv3:
                case R.id.ib3:
                    selectTab(2); break;
            }
        }
    };
    private void initEvents() {
        ib1.setOnClickListener(onClickListener);
        ib2.setOnClickListener(onClickListener);
        ib3.setOnClickListener(onClickListener);
        tv1.setOnClickListener(onClickListener);
        tv2.setOnClickListener(onClickListener);
        tv3.setOnClickListener(onClickListener);
    }
    private void initFragment() {
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new Fragment1());
        listfragment.add(new Fragment2());
        listfragment.add(new Fragment3());
        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) { return listfragment.get(position);
            }
            @Override
            public int getCount() { return listfragment.size();}
        };
        vp1.setAdapter(fpadapter);
        vp1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {}
            @Override
            public void onPageSelected(int position) {
                vp1.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }
}